import axios from "axios";
import store from "@/store"
import router from "@/router"
import { Message } from "element-ui";
//本地
axios.defaults.baseURL = "http://127.0.0.1:8888/api/private/v1/"
//线上
// axios.defaults.baseURL = "http://82.156.30.240:8888/api/private/v1/"

//请求前 拦截
axios.interceptors.request.use(function (config) {
  // Do something before request is sent

  // 发送 请求头 token
  config.headers.Authorization = store.getters.token;

  return config;
}, function (error) {
  // Do something with request error
  return Promise.reject(error);
});

// 响应拦截
// Add a response interceptor
axios.interceptors.response.use(function (response) {
  // Do something with response data

  let success = [200,201,204]
  if (response.data.meta.msg == "无效token") {
    router.push("/login")
    store.commit("user/DEL_TOKEN")
    store.commit("user/DEL_USERNAME")
  }else if (success.includes(response.data.meta.status)){
    Message.success(response.data.meta.msg)
  }else{
    Message.error(response.data.meta.msg)
  }

  return response.data;


}, function (error) {
  // Do something with response error
  return Promise.reject(error);
});


export default axios;